<template>
	<div>
		<el-table ref="scrollTable" :data="jsonDataArray" height="250" style="width: 100%;" @mouseenter="stopScroll" @mouseleave="startScroll">
			<el-table-column label="编号" prop="uid" :width="60"></el-table-column>
			<el-table-column label="卫星ip" prop="ip" :width="78"></el-table-column>
			<el-table-column label="高度(km)" prop="alt" :width="85"></el-table-column>
			<el-table-column label="纬度(°)" prop="lat" :width="70"></el-table-column>
			<el-table-column label="经度(°)" prop="lon"></el-table-column>
			<el-table-column label="CPU资源(m)" prop="cpu" :width="70"></el-table-column>
			<el-table-column label="内存资源(Mi)" prop="memory" :width="70"></el-table-column>
		</el-table>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				scrollIntervalId: null, // 保存滚动定时器ID
				dataIntervalId: null, // 保存数据获取定时器ID
				jsonDataArray: [],
				jsonData: {},
				shouldFetchData: true, // 控制是否继续获取数据
			};
		},
		methods: {
			getData() {
				let that = this;
				that.$axios.get("http://10.112.38.20:39898/data").then(function(res) {
					that.jsonDataArray = Object.values(res.data);
				});
			},
			// 开始滚动
			startScroll() {
				if (!this.scrollIntervalId) {
					this.scrollIntervalId = setInterval(() => {
						const firstItem = this.jsonDataArray.shift();
						if (firstItem) {
							this.jsonDataArray.push(firstItem);
						}
					}, 1000); // 每1000毫秒滚动一次
				}
			},
			// 停止滚动
			stopScroll() {
				if (this.scrollIntervalId) {
					clearInterval(this.scrollIntervalId);
					this.scrollIntervalId = null;
					this.getData();
				}
			},
			switper() {
				let that = this;
				const timerID = setInterval(() => {
					// console.log(that.jsonDataArray)
					// console.log(that.jsonDataArray[123])
					// 检查 jsonDataArray 是否为空
					if (that.jsonDataArray.length === 0) {
						// 如果为空，调用 getData 方法
						that.getData();
					}
				}, 3000); // 每3秒检查一次
			},
		},
		// 组件创建后填充数据并开始滚动
		created() {
			this.switper();
			this.startScroll();
		},
		// 组件销毁前停止滚动
		beforeDestroy() {
			this.stopScroll(); // 组件销毁前停止滚动
			if (this.dataIntervalId) {
				clearInterval(this.dataIntervalId);
				this.dataIntervalId = null;
			}
		},
	};
</script>

<style lang="scss" scoped>
	@import "./table.scss";
	
</style>
